/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "testClient.h"
#include <map>
#include <string>
#include <unistd.h>
#include <iostream>
#include "concurrent_task_log.h"
#include "concurrent_task_client.h"

using namespace OHOS::ConcurrentTask;
using namespace std;

constexpr int SLEEP_TIME = 100000;

static void StartApp(int uid)
{
    std::unordered_map<std::string, std::string> payload;
    payload["uid"] = std::to_string(uid);
    payload["type"] = "appStart";
    OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().ReportData(
        0, uid, payload);
}

static void SwapToFront(int uid)
{
    std::unordered_map<std::string, std::string> payload;
    payload["uid"] = std::to_string(uid);
    payload["type"] = "foreground";
    OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().ReportData(
        0, uid, payload);
}

static void SwapToBackground(int uid)
{
    std::unordered_map<std::string, std::string> payload;
    payload["uid"] = std::to_string(uid);
    payload["type"] = "background";
    OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().ReportData(
        0, uid, payload);
}

static void KillApp(int uid)
{
    std::unordered_map<std::string, std::string> payload;
    payload["uid"] = std::to_string(uid);
    payload["type"] = "background";
    OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().ReportData(
        0, uid, payload);
}

static void QueryUid(int item)
{
    IntervalReply rs;
    rs.rtgId = -1;
    cout << "start to query ui" <<endl;
    OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().QueryInterval(
        item, rs);
    cout << "Get ui rtgid: " << rs.rtgId << endl;
}

static void QueryRenderService(int item)
{
    IntervalReply rs;
    rs.rtgId = -1;
    cout << "start to query renderService" <<endl;
    OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().QueryInterval(
        item, rs);
    cout << "Get renderservice rtgid: " << rs.rtgId << endl;
}

int main(int argc, char *argv[])
{
    CONCUR_LOGI("concurrent_task_test: test start");
    int uid = getuid();
    int pid = getpid();
    if (uid == 0) {
        uid = pid;
    }
    int appUid = uid;
    cout << "simulator uid " << uid << " and pid " << pid << endl;
    usleep(SLEEP_TIME);
    QueryUid(QUERY_UI);
    QueryRenderService(QUERY_RENDER_SERVICE);
    usleep(SLEEP_TIME);
    cout << "simulator app start..." << endl;
    StartApp(appUid);
    cout << "App start finished!" << endl;
    usleep(SLEEP_TIME);
    cout << "App switch to front!" << endl;
    SwapToFront(appUid);
    usleep(SLEEP_TIME);
    QueryUid(QUERY_UI);
    QueryRenderService(QUERY_RENDER_SERVICE);
    usleep(SLEEP_TIME);
    cout << "App switch to background!" << endl;
    SwapToBackground(appUid);
    usleep(SLEEP_TIME);
    QueryUid(QUERY_UI);
    QueryRenderService(QUERY_RENDER_SERVICE);
    usleep(SLEEP_TIME);
    cout << "App switch to front!" << endl;
    SwapToFront(appUid);
    usleep(SLEEP_TIME);
    QueryUid(QUERY_UI);
    QueryRenderService(QUERY_RENDER_SERVICE);
    usleep(SLEEP_TIME);
    cout << "App switch to background!" << endl;
    SwapToBackground(appUid);
    usleep(SLEEP_TIME);
    KillApp(appUid);
    CONCUR_LOGI("concurrent_task_test: test end");
    QueryUid(QUERY_UI);
    QueryRenderService(QUERY_RENDER_SERVICE);
    return 0;
}